package com.beilang.base.property;


import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AppDefaultProperty {

    protected static final Log log = LogFactory.getLog(AppDefaultProperty.class);
    private CompositeConfiguration config;
    private static AppDefaultProperty instance;

    private AppDefaultProperty() {
        this.config = new CompositeConfiguration();
        this.loadConfiguration();
    }

    /**
     * Get the instance of the AppDefaultProperty
     *
     * @return AppDefaultProperty
     */
    public static AppDefaultProperty getInstance() {
        if (instance == null)
            instance = new AppDefaultProperty();
        return instance;
    }

    /**
     * Find the properties field with the key containing special key word
     */
    public static String getProperty(String name) {
        return getInstance().config.getString(name);
    }

    /**
     * Find the properties field with the key containing special key word, the
     * default value be return is no value found.
     */
    public static String getProperty(String name, String defaultValue) {
        if (getInstance().config.containsKey(name)) {
            return getInstance().config.getString(name, defaultValue);
        } else {
            return defaultValue;
        }
    }
    
    /**
     * Loads the properties from the configuration file.
     */
    public void loadConfiguration() {
        SystemConfiguration systemprop = new SystemConfiguration();
        config.addConfiguration(systemprop);
        ConfigurationFactory factory = new ConfigurationFactory();
        try {
            factory.setConfigurationURL(getClass().getClassLoader().getResource("config.xml"));
            config.addConfiguration(factory.getConfiguration());
        } catch (Exception e) {
            log.error("Unable to get configuration file config.xml, it is not a classpath resource.", e);
        }
    }

   
}
